package org.lzfto.flink.demo.system.config;

import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.lzfto.flink.demo.common.data.PageData;
import org.lzfto.flink.demo.common.data.PagingOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

@Lazy
@Repository
public class SysConfigRepository extends PagingOperation {

	private final Logger  LOGGER=LoggerFactory.getLogger(getClass());

	public String findByConfigKey(String configKey) {
		String sql = "SELECT config_value FROM sys_config WHERE config_key = ?";
		try {
			String value= jdbcTemplate.queryForObject(sql, String.class, configKey);
			return value;	
		}
		catch(Exception e) {
			LOGGER.error("不存在配置项目:{}",configKey);
			return null;
		}
	}
	
	public String findByConfigKey(String configKey,String defVal) {
		String sql = "SELECT config_value FROM sys_config WHERE config_key = ?";
		try {
			String value= jdbcTemplate.queryForObject(sql, String.class, configKey);
			return value;	
		}
		catch(Exception e) {
			LOGGER.error("不存在配置项目:{}",configKey);
			return defVal;
		}
	}

	public SysConfig insert(SysConfig sysConfig) {
		String sql = "INSERT INTO sys_config (config_key, config_value, comments) VALUES (?, ?, ?)";
		KeyHolder keyHolder = new GeneratedKeyHolder();
		jdbcTemplate.update(connection -> {
			PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			ps.setString(1, sysConfig.getConfigKey());
			ps.setString(2, sysConfig.getConfigValue());
			ps.setString(3, sysConfig.getComments());
			return ps;
		}, keyHolder);

		sysConfig.setId(keyHolder.getKey().intValue());
		return sysConfig;
	}

	public void update(SysConfig sysConfig) {
		String sql = "UPDATE sys_config SET config_value = ?, comments = ? WHERE config_key = ?";
		jdbcTemplate.update(sql, sysConfig.getConfigValue(), sysConfig.getComments(), sysConfig.getConfigKey());
	}

	public SysConfig selectById(int id) {
		String sql = "SELECT id ,config_key,config_value,comments FROM sys_config WHERE id = ?";
		BeanPropertyRowMapper<SysConfig> rowMapper = new BeanPropertyRowMapper<>(SysConfig.class);
		return jdbcTemplate.queryForObject(sql, rowMapper, id);
	}

	public PageData<SysConfig> selectByPage(int pageNo, int pageSize, SysConfig condition) {
		String sql = "SELECT * FROM sys_config WHERE 1=1 \n ";
		String pageSql = "select id ,config_key,config_value,comments from sys_config where 1=1 \n";
		String whereSql = "";
		List<Object> params = new ArrayList<>();
		if (condition != null) {

			if (condition.getConfigKey() != null && !condition.getConfigKey().isEmpty()) {
				whereSql += " \n AND config_key LIKE ?";
				params.add("%" + condition.getConfigKey() + "%");
			}
			if (condition.getConfigValue() != null && !condition.getConfigValue().isEmpty()) {
				whereSql += " AND config_value LIKE ?";
				params.add("%" + condition.getConfigValue() + "%");
			}
			if (condition.getComments() != null && !condition.getComments().isEmpty()) {
				whereSql += " AND comments LIKE ?";
				params.add("%" + condition.getComments() + "%");
			}
		}

		sql+=whereSql;
		pageSql+=whereSql;
		return this.buildPage(pageNo, pageSize, sql, pageSql, params, SysConfig.class);
	}

	public void deleteById(int id) {
		String sql = "DELETE FROM sys_config WHERE id = ?";
		jdbcTemplate.update(sql, id);
	}
}
